package com.fenghongzhang.module_home

import android.annotation.SuppressLint
import android.net.Uri
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.fenghongzhang.lib_base.core.BaseActivity
import com.fenghongzhang.lib_common.ARouterCons
import com.fenghongzhang.lib_xmpp.XmppManager
import com.fenghongzhang.module_home.adapter.HomePagerAdapter
import com.fenghongzhang.module_home.databinding.ActivityHomeBinding
import com.fenghongzhang.module_home.ui.hot.HotFragment
import com.fenghongzhang.module_home.ui.movie.MovieFragment
import com.fenghongzhang.module_home.ui.recommend.movie.RecommendFragment
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import org.jivesoftware.smack.chat2.ChatManager

@AndroidEntryPoint
@Route(path = ARouterCons.HOME_ACTIVITY)
class HomeActivity : BaseActivity<ActivityHomeBinding, HomeViewModel>(ActivityHomeBinding::inflate) {

    override val viewModel: HomeViewModel by viewModels()

    override fun initView() {
        viewModel.sendEvent(HomeIntent.GetSimpleType)
        //初始化IM . XMPP

        Thread{
            XmppManager.getInstance().xmppUserManager.login("xiaoming","xiaoming")

            ChatManager.getInstanceFor(XmppManager.getInstance().connection).addIncomingListener { from, message, chat ->
                println("from:" + message.from + "  to:" + message.to + " message body -> " + message.body)
//                if (message != null && !TextUtils.isEmpty(message.body)) {
//                    LogUtils.d("from:" + message.from + "  to:" + message.to + " message body -> " + message.body)
//                    val msgEntity: MsgEntity = MsgEntity()
//                    val ff = message.from.asEntityBareJidIfPossible().toString()
//                    msgEntity.setFrom(ff.substring(0, ff.lastIndexOf("@")))
//                    msgEntity.setTo(message.to.asBareJid().toString())
//                    msgEntity.setMsg(message.body)
//                    callback.Success(msgEntity)
//                }
            }
        }.start()


    }


    /**
     * 测试provider
     */
    private fun providerTest() {
        contentResolver.query(
            Uri.parse("content://com.fhz.a2207"),
            null,
            null,
            null,
            null
        )?.use {
            while (it.moveToNext()) {
                val id = it.getInt(0)
                val name = it.getString(1)
                val age = it.getInt(2)
                println("id:$id,name:$name,age:$age")
            }
        }
//        val contentResolver = contentResolver
//        val uri = Uri.parse("content://com.fhz.a2207")
//        val cursor = contentResolver.query(uri, null, null, null,null)
//        while (cursor.moveToNext()){
//            val id = cursor.getInt(0)
//            val name = cursor.getString(1)
//
//        }

    }

    private fun getFragment():List<Fragment> {
        return listOf(
            RecommendFragment(),
            MovieFragment(),
            HotFragment()
        )
    }

    override fun initData() {
        collectState(viewModel.state) { state ->
            updateUI(state)
        }
    }


    private fun updateUI(state: HomeUIState) {
        val fragments = getFragment()
        val tabLayoutNames = state.simpleType?.take(3)

        binding.apply {
            val tabLayout = tabLayout
            val viewPager = homeViewPager

            // 设置 ViewPager2 的适配器
            val adapter = HomePagerAdapter(this@HomeActivity,fragments)
            viewPager.adapter = adapter

            // 将 TabLayout 和 ViewPager2 进行绑定
            TabLayoutMediator(tabLayout, viewPager) { tab, position ->
                tab.text = tabLayoutNames?.get(position)?.typename
            }.attach()
        }


//        binding.apply {
//            if(state.isSucess){
//                println(state.simpleType.apply { Logger.d(this) })
//            }
//        }
    }
}


